package com.xlobo.api.inventory.testcase;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.xlobo.api.inventory.parameter.InventoryConfirmDetailBean;
import com.xlobo.api.inventory.parameter.InventoryConfirmRequestBean;
import com.xlobo.api.inventory.parameter.InventoryFreezeBean;
import com.xlobo.api.inventory.parameter.InventoryFreezeProductInfoBean;
import com.xlobo.api.inventory.parameter.InventoryPreFreezeBean;
import com.xlobo.api.inventory.service.InventoryConfirmRequestCall;
import com.xlobo.api.inventory.service.InventoryFreezeCall;
import com.xlobo.api.inventory.service.InventoryPreFreezeCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.database.model.Inventory;
import com.ymttest.database.sqlwapper.InventoryWapper;
import com.ymttest.utils.TestDataManager;

/**
 * 库存确认接口 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_InventoryConfirmRequest {
	private static InventoryConfirmRequestBean confirmBean;
	private static InventoryConfirmRequestCall confirmCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("库存确认接口");
	}

	@Before
	public void caseUp() {
		confirmBean = new InventoryConfirmRequestBean();
		confirmCall = new InventoryConfirmRequestCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(confirmCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	// call InventoryPreFreeze
	private static Map<String, Comparable> InventoryPreFreezeCallService() {
		InventoryPreFreezeBean preFreezebean = new InventoryPreFreezeBean();
		InventoryPreFreezeCall preFreezecall = new InventoryPreFreezeCall();
		Map<String, Comparable> perFreezeMap = new HashMap();
		InventoryWapper iw = new InventoryWapper();
		try {
			String preFreezeBizId = TestDataManager.getRandomUUID().toString();
			// List<Inventory>
			// InventoryList=iw.getInventoryInfoByUserid(userid);
			// String userId=InventoryList.get(1).getUserid();
			List<Inventory> InventoryList = iw.getTop10InventoryInfo();
			Random r = new Random();
			int index = r.nextInt(10);
			String userid = InventoryList.get(index).getUserid();
			Integer businessType = 111;
			Integer freezeCount = 2;
			Integer logisticId = InventoryList.get(index).getLogisticid();
			String skucode = InventoryList.get(index).getSkucode();
			ArrayList<InventoryFreezeProductInfoBean> productList = new ArrayList<InventoryFreezeProductInfoBean>();
			InventoryFreezeProductInfoBean productinfo = new InventoryFreezeProductInfoBean();
			productinfo.setSkucode(skucode);
			productinfo.setFreezeCount(freezeCount);
			productList.add(productinfo);
			preFreezebean.setProductInfos(productList);
			preFreezebean.setBusinessId(preFreezeBizId);
			preFreezebean.setBusinessType(businessType);
			preFreezebean.setComment("备注");
			preFreezebean.setUserId(userid);
			preFreezecall.setData(preFreezebean);
			preFreezecall.callService();
			Logger.comment("发送appcall报文:" + preFreezecall.getEntityString());
			Logger.comment("返回appcall报文：" + preFreezecall.getReturnData());
			perFreezeMap.put("userId", userid);
			perFreezeMap.put("freezeCount", freezeCount);
			perFreezeMap.put("logisticId", logisticId);
			perFreezeMap.put("skucode", skucode);
			perFreezeMap.put("preFreezeBizId", preFreezeBizId);
		} catch (Exception e) {
			e.printStackTrace();
			;
		}
		return perFreezeMap;
	}

	// call InventoryFreeze
	private static Map<String, Comparable> InventoryFreezeCallService() {
		InventoryFreezeBean freezeBean = new InventoryFreezeBean();
		InventoryFreezeCall freezeCall = new InventoryFreezeCall();
		Map<String, Comparable> perFreezeMap = InventoryPreFreezeCallService();
		Map<String, Comparable> freezeMap = new HashMap();
		try {
			String freezeBizId = TestDataManager.getRandomUUID().toString();
			String userId = (String) perFreezeMap.get("userId");
			Integer businessType = 111;
			String preFreezeBizId = (String) perFreezeMap.get("preFreezeBizId");
			Integer freezeCount = (Integer) perFreezeMap.get("freezeCount");
			Integer logisticId = (Integer) perFreezeMap.get("logisticId");
			String skucode = (String) perFreezeMap.get("skucode");
			ArrayList<InventoryFreezeProductInfoBean> freezeProductList = new ArrayList<InventoryFreezeProductInfoBean>();
			InventoryFreezeProductInfoBean productinfo = new InventoryFreezeProductInfoBean();
			productinfo.setSkucode(skucode);
			productinfo.setFreezeCount(freezeCount);
			productinfo.setOrderNo(preFreezeBizId);
			freezeProductList.add(productinfo);
			freezeBean.setLogisticId(logisticId);
			freezeBean.setProductInfos(freezeProductList);
			freezeBean.setBusinessId(freezeBizId);
			freezeBean.setBusinessType(111);
			freezeBean.setComment("备注");
			freezeBean.setUserId(userId);
			freezeCall.setData(freezeBean);
			freezeCall.callService();
			Logger.comment("发送appcall报文:" + freezeCall.getEntityString());
			Logger.comment("返回appcall报文：" + freezeCall.getReturnData());
			freezeMap.put("userId", userId);
			freezeMap.put("freezeCount", freezeCount);
			freezeMap.put("logisticId", logisticId);
			freezeMap.put("skucode", skucode);
			freezeMap.put("freezeBizId", freezeBizId);
		} catch (Exception e) {
			e.printStackTrace();
			;
		}
		return freezeMap;
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_InventoryConfirmRequest_001() {
		Logger.start(true,"库存确认成功");
		try {
			Map<String, Comparable> freezeMap = InventoryFreezeCallService();
			String confirmBizId = TestDataManager.getRandomUUID().toString();
			String userId = (String) freezeMap.get("userId");
			Integer businessType = 111;
			String freezeBizId = (String) freezeMap.get("freezeBizId");
			Integer freezeCount = (Integer) freezeMap.get("freezeCount");
			Integer logisticId = (Integer) freezeMap.get("logisticId");
			String skucode = (String) freezeMap.get("skucode");
			ArrayList<InventoryConfirmDetailBean> confirmInfoList = new ArrayList<InventoryConfirmDetailBean>();
			InventoryConfirmDetailBean confirmInfo = new InventoryConfirmDetailBean();
			confirmInfo.setConfirmCount(freezeCount);
			confirmInfo.setSkucode(skucode);
			confirmInfoList.add(confirmInfo);
			confirmBean.setLogisticId(logisticId);
			confirmBean.setContextBusinessId(freezeBizId);
			confirmBean.setContextBusinessType(businessType);
			confirmBean.setConfirmDetails(confirmInfoList);
			confirmBean.setBusinessId(confirmBizId);
			confirmBean.setBusinessType(businessType);
			confirmBean.setComment("备注");
			confirmBean.setUserId(userId);
			confirmCall.setData(confirmBean);
			confirmCall.callService();
			Logger.comment("发送appcall报文:" + confirmCall.getEntityString());
			Logger.comment("返回appcall报文：" + confirmCall.getReturnData());
			Logger.verifyEquals("true", confirmCall.getSuccess(), "验证Code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_InventoryConfirmRequest_002() {
		Logger.start(true,"不输入必填项LogisticId失败");
		try {
			// TODO 补全case代码
			org.junit.Assert.fail();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

}